## Deserving Government Assistance? 
## Public Support for Aid to Struggling Firms and Workers
#############################################################
## Chris Witko
## Tobias Heinrich
#############################################################
## 6) Descriptive statistics
#############################################################

## Load prepped data
####################
data <- read.csv(file="output/Prepped_survey_data.csv")
CCES <- read.csv(file="output/Prepped_CCES_data.csv")

## Resample CCES to make target moments "correct" (ie. respecting sample weights)
CCES <- CCES[sample(1:nrow(CCES), size=nrow(CCES), prob=CCES$w), ]


## Worker data
##############
data_worker <- subset(data, Which_experiment == "Worker")
data_firm <- subset(data, Which_experiment == "Firm")
CCES$id <- sample(1:10, size=nrow(CCES), replace=TRUE)
eb_form <- id ~ 1 + Gender_female + Age + Education_uni + I(Party == "Democrat") + I(Party == "Republican") +
  + I(Job == "Full-time") + I(Job == "Unemployed")
X_cces <- model.matrix(eb_form, CCES)[, -1]
X_data_worker <- model.matrix(eb_form, data_worker)[, -1]
X_data_firm <- model.matrix(eb_form, data_firm)[, -1]

X_eb_worker <- rbind(X_data_worker, X_cces)
X_eb_firm <- rbind(X_data_firm, X_cces)
eb_worker <- ebalance(Treatment=c(rep(0, nrow(data_worker)), rep(1, nrow(X_cces))), X=X_eb_worker) 
eb_worker <- ebalance.trim(eb_worker)
w_worker <- eb_worker$w / sum(eb_worker$w) * nrow(data_worker)

eb_firm<- ebalance(Treatment=c(rep(0, nrow(data_firm)), rep(1, nrow(X_cces))), X=X_eb_firm) 
eb_firm <- ebalance.trim(eb_firm)
w_firm <- eb_firm$w / sum(eb_firm$w) * nrow(data_firm)

bal <- rbind(data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Gender, female",
                        Y=c(weighted.mean(CCES$Gender_female, CCES$w), mean(data_worker$Gender_female), weighted.mean(data_worker$Gender_female, w=w_worker),
                            mean(data_firm$Gender_female), weighted.mean(data_firm$Gender_female, w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Age",
                        Y=c(weighted.mean(CCES$Age, CCES$w), mean(data_worker$Age), weighted.mean(data_worker$Age, w=w_worker),
                            mean(data_firm$Age), weighted.mean(data_firm$Age, w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Education", 
                        Y=c(weighted.mean(CCES$Education_uni, CCES$w), mean(data_worker$Education_uni), weighted.mean(data_worker$Education_uni, w=w_worker),
                            mean(data_firm$Education_uni), weighted.mean(data_firm$Education_uni, w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Party, Democrat", 
                        Y=c(weighted.mean(CCES$Party == "Democrat", CCES$w), mean(data_worker$Party == "Democrat"), weighted.mean(data_worker$Party == "Democrat", w=w_worker),
                            mean(data_firm$Party == "Democrat"), weighted.mean(data_firm$Party == "Democrat", w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Party, Republican", 
                        Y=c(weighted.mean(CCES$Party == "Republican", CCES$w), mean(data_worker$Party == "Republican"), weighted.mean(data_worker$Party == "Republican", w=w_worker),
                            mean(data_firm$Party == "Republican"), weighted.mean(data_firm$Party == "Republican", w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Job, full-time", 
                        Y=c(weighted.mean(CCES$Job == "Full-time", CCES$w), mean(data_worker$Job == "Full-time"), weighted.mean(data_worker$Job == "Full-time", w=w_worker),
                            mean(data_firm$Job == "Full-time"), weighted.mean(data_firm$Job == "Full-time", w=w_firm))),
             data.frame(What=c("CCES", "Worker, raw", "Worker, EB", "Firm, raw", "Firm, EB"), Variable="Job, unemployed", 
                        Y=c(weighted.mean(CCES$Job == "Unemployed", CCES$w), mean(data_worker$Job == "Unemployed"), weighted.mean(data_worker$Job == "Unemployed", w=w_worker),
                            mean(data_firm$Job == "Unemployed"), weighted.mean(data_firm$Job == "Unemployed", w=w_firm))))

bal$Data <- ifelse(grepl(x=bal$What, pattern="CCES") == TRUE, "CCES",
                   ifelse(grepl(x=bal$What, pattern="Worker") == TRUE, "Worker", "Firm"))
bal$Type <- ifelse(grepl(x=bal$What, pattern="CCES") == TRUE, "CCES",
                   ifelse(grepl(x=bal$What, pattern="EB") == TRUE, "Balanced", "Raw"))

g <- ggplot(data=bal, aes(x=Variable, colour=factor(Data), y=Y, shape=Type))
g <- g + geom_point(size=3, position=position_dodge(width=0.3))
g <- g + theme_minimal() + ylab("Mean/ proportion")
g <- g + scale_colour_grey(name="Data")
g <- g + xlab("Demographic variable")
g <- g + theme(axis.text.y=element_text(hjust=0, size=rel(1.34)),
               axis.text.x=element_text(size=rel(1.34)))
ggsave("output/Descriptives.pdf", width=11, height=5.2, device=cairo_pdf)
rm(bal)

## Marginal means
#################
data_worker$w <- w_worker
data_firm$w <- w_firm
u_data <- list(Firm=data_firm, Worker=data_worker)
u_names <- list(Firm=c("F_typicalworkerage", "F_numberofworkers", "F_reasonforstruggle", "F_typicalworkerraceethnicity",
                       "F_typicalworkersalary", "F_industry", "F_typicalworkereducation", "F_ceopay"),                 
                Worker=c("W_salary", "W_reasonforjobloss", "W_raceethnicity", "W_age", "W_gender", "W_education"))

storage <- c()
for(j in 1:2)
{
  this_data <- u_data[[j]]
  
  for(l in 1:length(u_names[[j]]))
  {
    out <- ddply(.data=this_data, .variables=u_names[[j]][l],
                 .fun=function(x) data.frame(What=ifelse(j == 1, "Firm", "Worker"),
                                             Variable=u_names[[j]][l],
                                             Mean=c(mean(x$Choice), weighted.mean(x$Choice, x$w)),
                                             SE=c(sd(x$Choice)/sqrt(nrow(x)),
                                                  weightedSd(x$Choice)/sqrt(nrow(x))),
                                             Weights=c("No", "Yes")))
    colnames(out)[1] <- "Realization"
    storage <- rbind(storage, out)
  }
}
storage <- na.omit(storage)


## Rope ladder
storage$Q025 <- storage$Mean - 1.96 * storage$SE
storage$Q975 <- storage$Mean + 1.96 * storage$SE
storage$Sig <- ifelse(storage$Q025 > 0.5 | storage$Q975 < 0.5, "Sig", "Insig")
storage$Sig[storage$Header == 1] <- "Other"
storage$Sig[storage$Q975 == 0] <- "Other"

storage$Variable <- mapvalues(x=storage$Variable,
                              from=c("F_typicalworkerage", "F_numberofworkers", "F_reasonforstruggle",
                                     "F_typicalworkerraceethnicity", "F_typicalworkersalary", "F_industry",
                                     "F_typicalworkereducation", "F_ceopay", "W_reasonforjobloss",
                                     "W_raceethnicity", "W_age", "W_gender", "W_education", "W_salary"),
                              to=c("Workers ages", "Number of workers", "Reason for struggle",
                                   "Workers race/ ethnicity", "Workers salary", "Industry", "Worker education",
                                   "CEO pay", "Reason for job loss", "Race/ ethnicity", "Age", "Gender", "Education", "Salary"))

storage$Realization2 <- mapvalues(x=storage$Realization, 
                                  from=c("Laid off due to domestic competition", "Laid off due to foreign competition",
                                         "Operations were moved to a foreign country", 
                                         "Laid off due to company mismanagement", "Fired for poor performance", 
                                         "Job was replaced by technology (like robot, artificial intelligence)",
                                         "Laid off due to economic recession", "Company was closed due to pandemic public health restrictions",
                                         "Economic recession", "Competition due to superior use of technology (like robots, artificial intelligence)",
                                         "Mismanagement", "Foreign competition due to lower labor cost",
                                         "Operations closed due to pandemic public health restrictions"),
                                  to=c("Domestic competition", "Foreign competition", "Offshoring",  
                                       "Company mismanagement", "Poor performance",
                                       "Technology", "Recession", "Public health",
                                       "Recession", "Technology", "Mismanagement", "Foreign competiton", "Public health"))


for(j in 1:2)
{
  for(i in 1:2)
  {
    if(i == 1) 
    {
      tmp <- subset(storage, What == "Worker" & Weights == ifelse(j == 1, "Yes", "No"))
      tmp$Realization2 <- factor(tmp$Realization2,
                                 levels=c("Public health", "Poor performance", "Technology", "Company mismanagement", 
                                          "Domestic competition", "Recession", "Foreign competition", "Offshoring",     
                                          "$20,000", "$70,000", "$160,000",
                                          "Asian", "Black", "Hispanic", "White", "Native American", "Mixed", "Other", 
                                          "20s", "30s", "40s", "50s", "60s", "Female", "Male", "4-year university", "Less than 4-year university"))
    }
    if(i == 2) 
    {
      tmp <- subset(storage, What == "Firm" & Weights == ifelse(j == 1, "Yes", "No"))
      tmp$Realization2 <- factor(tmp$Realization2,
                                 levels=c("20s", "30s", "40s", "50s", "60s", "Less than 5", "Between 5 and 9", "Between 10 and 19",
                                          "Between 20 and 99", "Between 100 and 499", "500 or more",
                                          "Technology", "Recession", "Foreign competiton", "Mismanagement", "Public health",
                                          "Asian", "Black", "Hispanic", "Mixed", "Native American", "Other", "White",
                                          "$20,000", "$70,000", "$160,000",
                                          "Arts, entertainment, recreation", "Construction", "Finance and insurance", "Health care",
                                          "Manufacturing", "Real estate and rental", "Retail", "Transportation and warehousing",
                                          "Travel and food services", "Wholesale trade", "4-year university", "Less than 4-year university",
                                          "$200,000", "$2 million", "$10 million"))  
    }
    
    ## Rescale
    tmp[, c("Mean", "Q025", "Q975")] <- 100 * tmp[, c("Mean", "Q025", "Q975")]
    tmp$Mean_label <- format(round(tmp$Mean, di=0), nsmall=0)
    
    g <- ggplot(data=tmp, aes(x=Realization2, y=Mean))
    g <- g + geom_hline(yintercept=50, size=0.8)
    g <- g + coord_flip() + theme_minimal() + facet_wrap(~ Variable, ncol=2, scales="free_y")
    g <- g + geom_linerange(data=subset(tmp, Sig == "Sig"),
                            aes(x=Realization2, ymin=Q025, ymax=Q975), colour="black", linewidth=0.9)
    g <- g + geom_linerange(data=subset(tmp, Sig == "Sig"),
                            aes(x=Realization2, ymin=Q025, ymax=Q975), colour="black", linewidth=.9)
    g <- g + geom_linerange(data=subset(tmp, Sig == "Insig"),
                            aes(x=Realization2, ymin=Q025, ymax=Q975), colour="gray60", linewidth=.9)
    g <- g + geom_point(data=subset(tmp, Sig == "Insig"), size=3, colour="gray60")
    g <- g + geom_label(data=subset(tmp, Sig == "Sig"), aes(label=Mean_label), size=3.1)
    g <- g + theme(axis.text.y=element_text(hjust=1, size=rel(1.24)),
                   axis.text.x=element_text(size=rel(1.24)),
                   strip.text=element_text(hjust=0, size=rel(1.34)),
                   plot.title=element_text(size=rel(1.53), hjust=0), 
                   legend.position="none") 
    g <- g + xlab("") + ylab("Marginal means")
    if(i == 1) ggsave(paste0("output/MMs-Workers-", ifelse(j == 1, "weighted", "unweighted"), ".pdf"), width=11, height=8.2, device=cairo_pdf)
    if(i == 2) ggsave(paste0("output/MMs-Firms-", ifelse(j == 1, "weighted", "unweighted"), ".pdf"), width=11, height=8.2, device=cairo_pdf)
  }
}


## Garbage collection
#####################
rm(list=ls())
